/**
 * @file name PasswordActivity.java
 */
package com.digital.community;

import java.util.HashMap;

import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;

import com.digital.community.common.utils.HttpUtils;
import com.digital.community.common.utils.StringUtils;
import com.digital.community.common.utils.ToastUtils;

/**
 * 
 * @author yarine.gao
 * @date 2012-11-11
 * @version 1.0.0
 */
public class PasswordActivity extends Activity {

	private EditText pwdET;
	private EditText confirmET;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tbpassword);
		uguid = getIntent().getStringExtra("uguid");
		
		System.out.println("uguid: " + uguid);
		
		pwdET = (EditText)findViewById(R.id.et_newpwd);
		confirmET = (EditText)findViewById(R.id.et_confirm);
	}
	
	private String uguid = null;
	private String pwd1 = null;
	private String pwd2 = null;
	public void update(View view) {
		
		if(StringUtils.isBlank(uguid)) {
			ToastUtils.showErrorToast(this, getLayoutInflater(), "用户信息获取失败,请重新登录!", false);
			return;
		}
		
		pwd1 = pwdET.getText().toString();
		pwd2 = confirmET.getText().toString();
		
		if(StringUtils.isBlank(pwd1) || StringUtils.isBlank(pwd2)) {
			ToastUtils.showErrorToast(this, getLayoutInflater(), "两次输入不能为空!", false);
			return;
		}
		
		if(!pwd1.equals(pwd2)) {
			ToastUtils.showErrorToast(this, getLayoutInflater(), "两次输入不相同!", false);
			return;
		}
		
		toUpdate();
	}
	
	ProgressDialog proDialog;
	private void toUpdate() {
		proDialog = ProgressDialog.show(PasswordActivity.this, "请等待", "正在请求数据...",true,true);
		new Thread() {
			public void run() {
				HashMap<String, String> params = new HashMap<String, String>();
				params.put("pwd", pwd1);
				params.put("uguid", uguid);
				Bundle bundle = HttpUtils.getPost("updatePwd.do", params, 3000, PasswordActivity.this);
				if(bundle.getInt("code") == HttpUtils.SUCCESS) {
					String content = bundle.getString("content");
					if(StringUtils.isNotBlank(content)) {
						try {
							JSONObject obj = new JSONObject(content);
							int success = obj.getInt("success");
							if(success == 1) {
								handler.sendEmptyMessage(HANLER_UPDATE_SUCCESS);
							} else {
								handler.sendEmptyMessage(HANLER_UPDATE_FAILD);
							}
						} catch (Exception e) {
							System.out.println(e);
							handler.sendEmptyMessage(HANLER_CONN_ERROR);
						}
					} else {
						handler.sendEmptyMessage(HANLER_CONN_ERROR);
					}
				} else {
					handler.sendEmptyMessage(HANLER_CONN_ERROR);
				}
			};
		}.start();
	}
	
	private static final int HANLER_CONN_ERROR = 1;
	private static final int HANLER_UPDATE_SUCCESS = 2;
	private static final int HANLER_UPDATE_FAILD = 3;
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if(proDialog != null && proDialog.isShowing()) {
				proDialog.dismiss();
			}
			
			switch (msg.what) {
			case HANLER_CONN_ERROR:
				showDialog(DIALOG_NETWORK_ERROR);
				break;
			case HANLER_UPDATE_SUCCESS:
				ToastUtils.showTrueToast(PasswordActivity.this, getLayoutInflater(), "更新成功!", false);
				break;
			case HANLER_UPDATE_FAILD:
				showDialog(DIAL0G_UPDATE_FAILD);
				break;
			default:
				break;
			}
			
			
		};
	};
	
	
	private static final int DIALOG_NETWORK_ERROR = 200;
	private static final int DIAL0G_UPDATE_FAILD = 201;
	@Override
	protected Dialog onCreateDialog(int id) {
		if(!isActive) return null;
		switch (id) {
		case DIALOG_NETWORK_ERROR:
			return new AlertDialog.Builder(this).setTitle("提示信息").setMessage("对不起,网络联接失败!")
			.setPositiveButton("重试", new Retry())
			.setNegativeButton("取消", new Cancle()).create();
		case DIAL0G_UPDATE_FAILD:
			return new AlertDialog.Builder(this).setTitle("提示信息").setMessage("对不起,更新失败是否重试?")
			.setPositiveButton("重试", new Retry())
			.setNegativeButton("取消", new Cancle()).create();
		default:
			return null;
		}
		
	}
	
	private class Retry implements OnClickListener {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			toUpdate();
		}
	}
	private class Cancle implements OnClickListener {
		@Override
		public void onClick(DialogInterface dialog, int which) {
		}
	}
	
	private boolean isActive = true;
	@Override
	protected void onDestroy() {
		isActive = false;
		
		super.onDestroy();
	}
}
